// components/blog-ctrl/blog-ctrl.js
import util from '../../utils/util.js'
let userInfo = {}

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    blogId: String,
    blog: Object
  },
  externalClasses: ['iconfont', 'icon-pinglun', 'icon-fenxiang'],

  /**
   * 组件的初始数据
   */
  data: {
    // 登录组件是否显示
    loginShow: false,
    // 底部弹出层是否显示
    modalShow: false,
    content: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onComment() {
      // 判断用户是否授权
      // wx.getSetting({
      //   success: (res) => {
      //     console.log('res:::', res)
      //     if (res.authSetting['scope.userInfo']) {
      //       wx.getUserInfo({
      //         success: (res) => {
      //           userInfo = res.userInfo
      //           // 显示评论弹出层
                
      //           this.triggerEvent('changeModalShow', { userInfo, blogId: this.properties.blogId, blog: this.properties.blog})
      //         }
      //       })
      //     } else {
      //       this.triggerEvent('changeLoginShow', { blogId: this.properties.blogId, blog: this.properties.blog })
      //     }
      //   }
      // })
      util.getUserInfo().then(res => {
        // this.loginSuccess({
        //   detail: res
        // })
        userInfo = res
        this.triggerEvent('changeModalShow', { userInfo, blogId: this.properties.blogId, blog: this.properties.blog })
      }).catch(err => {
        this.triggerEvent('changeLoginShow', { blogId: this.properties.blogId, blog: this.properties.blog })
      })
    },

    onLoginsuccess(event) {
      userInfo = event.detail
      // 授权框消失，评论框显示
      this.setData({
        loginShow: false,
      }, () => {
        this.setData({
          modalShow: true,
        })
      })
    },

    onLoginfail() {
      wx.showModal({
        title: '授权用户才能进行评价',
        content: '',
      })
    },
    subscribe(content) {
      wx.cloud.callFunction({
        name: 'sendMessage',
        data: {
          content,
          blogId: this.properties.blogId
        }
      }).then((res) => {
        console.log(res)
      })
    },
    onSend(event) {
      console.log(event)
      // 插入数据库
      let content = event.detail.value.content

      console.log(content)

      wx.requestSubscribeMessage({
        tmplIds: [app.globalData.COMMENT],
        success:(res) => {
          console.log('成功后的回调', res)
          // 推送模板消息
          if (res[app.globalData.COMMENT] == 'accept') {
            this.subscribe(content)
          } else if (res[app.globalData.COMMENT] == 'reject') {
            wx.showToast({
              icon: 'none',
              title: '拒绝授权，将无法接受到评论成功提醒'
            })
          }
        },
        complete: (res) => {
          console.log('res:::', res)
          
        },
        fail: (err) => {
          console.log(err)
        }
      })
      if (content.trim() == '') {
        wx.showModal({
          title: '评论内容不能为空',
          content: '',
        })
        return
      }
      wx.showLoading({
        title: '评论中',
        mask: true,
      })
      db.collection('blog-comment').add({
        data: {
          content,
          createTime: db.serverDate(),
          blogId: this.properties.blogId,
          nickName: userInfo.nickName,
          avatarUrl: userInfo.avatarUrl
        }
      }).then((res) => {
        
        

        wx.hideLoading()
        wx.showToast({
          title: '评论成功',
        })
        this.setData({
          modalShow: false,
          content: '',
        })

        // 父元素刷新评论页面
        this.triggerEvent('refreshCommentList')
      })
    },

  }
})